package com.ping.service;

import java.awt.image.RescaleOp;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.jfinal.plugin.activerecord.Record;
import com.ping.common.Json;
import com.ping.model.Brand;

public class BrandService {

	public static JSONObject findHotBrands(){
		List<Record> records = Brand.findHotBrands();
		JSONObject result = new JSONObject();
		try {
			JSONArray array = Json.ListToJsonArray(records);
			result.put("hots", array);
			return result;
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}

	public static JSONObject findBrandsExceptHot(int order){
		List<Record> records = Brand.findBrandsExceptHot(order);
		JSONObject result = new JSONObject();
		
		try {
			JSONArray  array = Json.ListToJsonArray(records);
			result.put("brands", array);
			return result;
		} catch (JSONException e) {
			// TODO: handle exception
			e.printStackTrace();
			return null;
		}
	}	
	
	public static JSONObject findBrandByCat(String cat){
		List<Record> brands = Brand.findBrandsByCat(cat);
		JSONObject result = new JSONObject();
		
		try {
			JSONArray array = Json.ListToJsonArray(brands);
			result.put("brands", array);
			return result;
		} catch (JSONException e) {
			// TODO: handle exception
			e.printStackTrace();
			return null;
		}
	}
	
	public static JSONObject findCats(){
		List<Record> cats = Brand.findCats();
		JSONObject result = new JSONObject();
		
		try {
			JSONArray array = Json.ListToJsonArray(cats);
			result.put("cats", array);
			return result;
		} catch (JSONException e) {
			// TODO: handle exception
			e.printStackTrace();
			return null;
		}
	}
}
